﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entity;
using DAL;
using Common;
using System.Data.Linq;
namespace BAL
{
    public class SubjectAbility
    {
        private static string CacheName = "AllSubjectAbility";
        /// <summary>
        /// 写入缓存
        /// </summary>
        /// <returns></returns>
        public static void ToCache()
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                CacheHelper.Refurbish(CacheName, db.SubjectAbilities.ToList(), 0, 60);
            }
        }
        public static IList<Models.SubjectAbility> CheckCache()
        {
            if (!CacheHelper.IsExist(CacheName)) { ToCache(); }
            return (IList<Models.SubjectAbility>)CacheHelper.GetCache(CacheName);
        }
        internal static SubjectAbilityInfo TransSubjectAbility2SubjectAbilityInfo(Models.SubjectAbility subjectAbility)
        {
            SubjectAbilityInfo subjectAbilityInfo = new SubjectAbilityInfo();

            Utility.SetObjectValueWithTheSamePropertyName(subjectAbilityInfo, subjectAbility);

            return subjectAbilityInfo;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="subjectInfo"></param>
        /// <returns></returns>
        public static SubjectAbilityInfo AddAbility(SubjectAbilityInfo subjectAbilityInfo)
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                Models.SubjectAbility subjectAbility = new Models.SubjectAbility();
                subjectAbilityInfo.CreateDate = DateTime.Now;
                Utility.SetObjectValueWithTheSamePropertyName(subjectAbility, subjectAbilityInfo);
                DatabaseHelper.Insert<Models.SubjectAbility>(subjectAbility);
                ToCache();
                return TransSubjectAbility2SubjectAbilityInfo(subjectAbility);
            }
        }

        public static void EditAbility(SubjectAbilityInfo subjectAbilityInfo)
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                try
                {
                    Models.SubjectAbility subjectAbility = db.SubjectAbilities.Single(b => b.Id == subjectAbilityInfo.Id);

                    Utility.SetObjectValueWithTheSamePropertyName(subjectAbility, subjectAbilityInfo);

                    db.SubmitChanges(ConflictMode.ContinueOnConflict);
                    ToCache();
                }
                catch (ChangeConflictException)
                {
                    foreach (ObjectChangeConflict occ in db.ChangeConflicts)
                    {
                        occ.Resolve(RefreshMode.KeepCurrentValues);
                    }
                }
            }
        }

        public static void DelAbility(int id)
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                DelAbility(new int[] { id });
            }
        }

        public static void DelAbility(int[] lstId)
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                if (lstId.Length > 0)
                {
                    string sql = string.Format("delete from SubjectAbility where id in ({0})", string.Join(",", lstId.ConvertToStringArray()));
                    db.ExecuteCommand(sql);
                    ToCache();
                }
            }
        }

        public static void DelAbilityBySubject(int SubjectId)
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                int[] ids = db.SubjectAbilities.Where(t => t.SubjectId == SubjectId).Select(t => t.Id).ToArray();
                DelAbility(ids);
                ToCache();
            }
        }

        public static SubjectAbilityInfo GetAbilityInfoById(int Id)
        {
            IList<Models.SubjectAbility> subjectAbilitys = CheckCache();
            Models.SubjectAbility subjectAbility = subjectAbilitys.Single(b => b.Id == Id);
            return TransSubjectAbility2SubjectAbilityInfo(subjectAbility);
        }

        public static bool CheckAbilityExistByKeyword(string keyword, int editid, int SubjectId)
        {
            IList<Models.SubjectAbility> subjectAbility = CheckCache();
            if (SubjectId == 0)
            {
                return subjectAbility.Where(t => t.Keyword == keyword && t.Id != editid).Count() > 0;
            }
            else
            {
                return subjectAbility.Where(t => t.Keyword == keyword && t.Id != editid && t.SubjectId == SubjectId).Count() > 0;
            }
        }

        public static IList<SubjectAbilityInfo> GetAbilityInfosBySubjectId(int subjectid)
        {
            IList<Models.SubjectAbility> subjectAbility = CheckCache();
            {
                return subjectAbility.Where(t => t.SubjectId == subjectid).OrderBy(p => p.Name).Select(t => TransSubjectAbility2SubjectAbilityInfo(t)).ToList();
            }
        }

        public static IList<SubjectAbilityInfo> GetAbilityInfos()
        {
            IList<Models.SubjectAbility> subjectAbility = CheckCache();
            return subjectAbility.Select(t => TransSubjectAbility2SubjectAbilityInfo(t)).ToList();
        }
    }
}
